<template>
  <div class="content">
    <div class="admin-title">
      <div>用户管理</div>
      <div class="button" style="margin-right: 20px">
        <el-button @click="analysis" :loading="analysis_load"
          >数据分析</el-button
        >
      </div>
    </div>
    <div class="table">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="timeon" label="日期" width="180">
          <template slot-scope="scope">
            {{ scope.row.timeon | dateFilter }}
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" width="180">
        </el-table-column>
        <el-table-column prop="scope" label="总分"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="created_form">
      <el-dialog
        title="完成情况"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
        fullscreen
      >
        <div id="content">
          <el-table :data="userData" style="width: 100%">
            <el-table-column
              prop="username"
              label="姓名"
              width="180"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="shool"
              label="学院"
              :filters="academic"
              :filter-method="filterAca"
              width="100"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="type"
              label="完成情况"
              align="center"
              :filters="[
                { text: '已完成', value: '1' },
                { text: '未完成', value: '0' },
              ]"
              :filter-method="filterTag"
            >
              <template slot-scope="scope">
                {{ scope.row.type == 1 ? "已完成" : "未完成" }}
              </template>
            </el-table-column>
            <el-table-column
              prop="ziping"
              label="基础分"
              sortable
              align="center"
            >
            </el-table-column>
            <el-table-column prop="sum" label="最终得分" sortable>
              <template slot-scope="scope">
                <span :style="scope.row.sum < 60 ? 'color:red' : ''">{{
                  scope.row.sum
                }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button size="mini" @click="audit(scope.row)"
                  >审核</el-button
                >
                <el-button size="mini" @click="addScope(scope.row)"
                  >加分</el-button
                >
                <el-button size="mini" @click="decScope(scope.row)"
                  >减分</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-dialog>
    </div>

    <!-- 加减分对话框 -->
    <div class="add_dec">
      <el-dialog
        :title="isAdd ? '加分' : '减分'"
        :visible.sync="dialogVisible2"
        width="30%"
        :before-close="handleClose2"
      >
        <el-form ref="form" :model="form" label-width="80px" :rules="rules">
          <el-form-item label="分数" prop="scope">
            <el-input v-model="form.scope"></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="input">
            <el-input v-model="form.input" type="textarea"></el-input>
          </el-form-item>
          <el-divider content-position="center" v-if="!isAdd"
            >红线指标</el-divider
          >
          <el-form-item label="红线指标" v-if="!isAdd">
            <el-checkbox-group v-model="form.question">
              <el-checkbox
                v-for="(item, index) in redIndex"
                :key="index"
                :label="item.id"
                name="type"
                >{{ item.text }}</el-checkbox
              >
            </el-checkbox-group>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="updataScope('form', form)"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>

    <!-- 审核弹框 -->
    <div class="audit">
      <el-dialog
        title="审核"
        :visible.sync="dialogVisible3"
        width="30%"
        :before-close="handleClose3"
      >
        <div class="audit_table">
          <el-table :data="auditData" border style="width: 100%">
            <el-table-column
              type="index"
              label="题号"
              width="50"
              align="center"
            >
            </el-table-column>
            <el-table-column prop="textarea" label="评分标准">
            </el-table-column>
            <el-table-column
              prop="fen"
              label="自评得分"
              align="center"
              width="130"
            >
              <template slot-scope="scope">
                <el-input-number
                  v-model="scope.row.fen"
                  :min="0"
                  label="描述文字"
                ></el-input-number
              ></template>
            </el-table-column>
          </el-table>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="(dialogVisible3 = false), leaderAudit(auditData)"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { addjianfen, addjiafen, alljiafen } from "@/api/audit";
import { allMetrics } from "@/api/metrics";
import { allwenjuan, alluserxinxi } from "@/api/rating";
import { allxueyuan } from "@/api/academic";
import { allneirongs, leader_change } from "@/api/leaderChange";
import { scoreStatistics } from "@/api/analysis";
import { set } from "js-cookie";
export default {
  data() {
    return {
      num: 1,
      dialogVisible3: false,
      dialogVisible2: false,
      dialogVisible: false,
      tableData: [],
      userData: [],
      academic: [],
      isAdd: 1,
      //加分减分描述
      form: {
        scope: "",
        input: "",
        question: [],
      },
      //审核数据
      auditData: [],

      redIndex: [],

      rules: {
        scope: [
          { required: true, message: "请输入增加/减少的分数", trigger: "blur" },
        ],
        input: [
          {
            required: true,
            message: "请对增加/减少的分数进行描述",
            trigger: "blur",
          },
        ],
      },

      //当前学院信息
      academicRow: "",
      // 用户openid
      openid: "",
      // 问卷id
      wjid: "",

      //部门个数
      // depaetmentNum: 0,

      percentage: "",

      //按钮加载
      analysis_load: false,
    };
  },

  filters: {
    dateFilter(value) {
      return value.split(" ")[0];
    },
  },

  created() {
    this.init();
    return;
    // this.getDepaetment();
  },

  methods: {
    init() {
      this.getAllQuestionnaire();
    },

    //获取部门
    // getDepaetment() {
    //   allxueyuan().then((res) => {
    //     this.depaetmentNum = res.data.length;
    //   });
    // },

    //获取所有问卷
    getAllQuestionnaire() {
      allwenjuan().then((res) => {
        this.tableData = res.data.reverse();
        // return;
        // for (let i = 0; i < 100; i++) {
        //   if (this.depaetmentNum != 0) {
        //     this.getMonthWenjuan(res.data);
        //     break;
        //   }
        // }
      });
    },

    //获取时间
    // getMonthWenjuan(data) {
    //   function getNowFormatDate() {
    //     let date = new Date(),
    //       year = date.getFullYear(), //获取完整的年份(4位)
    //       month = date.getMonth() + 1; //获取当前月份(0-11,0代表1月)
    //     // strDate = date.getDate(); // 获取当前日(1-31)
    //     if (month < 10) month = `0${month}`; // 如果月份是个位数，在前面补0
    //     // if (strDate < 10) strDate = `0${strDate}`; // 如果日是个位数，在前面补0

    //     return `${year}-${month}`;
    //   }
    //   // console.log(getNowFormatDate());

    //   function fuzzyQuery(list, keyWord) {
    //     var arr = list.filter((item) => {
    //       // console.log(item);
    //       return item.endtime.indexOf(keyWord) >= 0;
    //     });
    //     return arr;
    //   }
    //   let thisMonth = fuzzyQuery(data, getNowFormatDate());
    //   console.log(thisMonth);
    //   if (thisMonth.length > 1) {
    //     let i = thisMonth.length * this.depaetmentNum;
    //     var v = [];

    //     thisMonth.forEach((a) => {
    //       let g;
    //       g = {
    //         id: a.id,
    //       };

    //       scoreStatistics(g).then((res) => {
    //         res.data.forEach((element) => {
    //           if (element.shool == null && element.shool == null) {
    //             return;
    //           } else {
    //             v.push(element.sum);
    //           }
    //         });
    //       });
    //     });
    //     setTimeout(() => {
    //       this.percentage = (v.length / i) * 100;
    //       // this.percentage.rate = v;
    //     }, 10000);
    //   }
    // },

    //获取完成情况和学院
    handleClick(row) {
      this.$router.push({
        name: "Accomplish",
        params: {
          title: row.title,
          id: row.id,
          standard: row.standard,
          count: row.count,
        },
      });
      return;
      this.dialogVisible = true;
      this.wjid = row.id;
      let id = {
        wjid: row.id,
      };
      alluserxinxi(id).then((res) => {
        // res.data.forEach((Element) => {
        //   this.userData.push(Element);
        // });
        this.userData = res.data;
      });
      allxueyuan().then((res) => {
        let academic = [];
        res.data.forEach((element) => {
          let a = {
            text: element.named,
            value: element.named,
          };
          academic.push(a);
        });
        this.academic = academic;
      });
    },

    handleClose(done) {
      this.userData = [];
      done();
    },

    handleClose2(done) {
      if (this.form.scope != "" || this.form.input != "") {
        this.$confirm("你还未提交信息，确认关闭？")
          .then((_) => {
            done();
            this.$refs.form.resetFields();
          })
          .catch((_) => {});
      } else {
        done();
      }
    },

    handleClose3(done) {
      done();
    },

    //筛选完成情况
    filterTag(value, row, column) {
      return row.type == value;
    },

    //筛选班级
    filterAca(value, row) {
      return row.shool == value;
    },

    //审核
    audit(row) {
      this.openid = row.id;
      let date = {
        xueyuan: row.shool,
        id: this.wjid,
      };
      allneirongs(date).then((res) => {
        this.auditData = res.data.slice(0, -1);
      });
      this.dialogVisible3 = true;
    },

    //自评加分
    addScope(row) {
      this.academicRow = row;
      this.isAdd = 1;
      let a = {
        openid: row.id,
        wjid: this.wjid,
      };
      alljiafen(a).then((res) => {
        let row = {
          scope: res.data.jiafen,
          input: res.data.jiayuan,
        };
        this.form = Object.assign({}, row);
      });
      this.dialogVisible2 = true;
    },

    //自评减分
    decScope(row) {
      this.academicRow = row;
      this.isAdd = 0;
      allMetrics().then((res) => {
        this.redIndex = res.data;
      });
      let a = {
        openid: row.id,
        wjid: this.wjid,
      };
      alljiafen(a).then((res) => {
        if (res.data.zhibiao == null) {
          let row = {
            scope: res.data.jianfen,
            input: res.data.jianyuan,
            question: [],
          };
          this.form = Object.assign({}, row);
        } else {
          let arr = JSON.parse(res.data.zhibiao);
          let row = {
            scope: res.data.jianfen,
            input: res.data.jianyuan,
            question: arr,
          };
          this.form = Object.assign({}, row);
        }
      });
      this.dialogVisible2 = true;
    },

    //加分/减分提交
    updataScope(form, data) {
      if (this.isAdd == 1) {
        data.wjid = this.wjid;
        data.openid = this.academicRow.id;
        data.xueyuan = this.academicRow.shool;
        delete data.question;
        addjiafen(data).then((res) => {
          if (res.code == 0) {
            this.dialogVisible2 = false;
            this.$refs.form.resetFields();
            let id = {
              wjid: this.wjid,
            };
            alluserxinxi(id).then((res) => {
              this.userData = res.data;
            });
            this.$message({
              type: "success",
              message: "加分成功",
            });
          }
        });
      } else {
        data.wjid = this.wjid;
        data.openid = this.academicRow.id;
        data.xueyuan = this.academicRow.shool;
        if (data.question.length != 0) {
          let type = [];
          for (let i = 0; i < data.question.length; i++) {
            let a = {
              pid: data.question[i],
            };
            type.push(a);
          }
          data.question = type;
        }
        addjianfen(data).then((res) => {
          if (res.code == 0) {
            this.dialogVisible2 = false;
            this.$refs.form.resetFields();
            let id = {
              wjid: this.wjid,
            };
            alluserxinxi(id).then((res) => {
              this.userData = res.data;
            });
            this.$message({
              type: "warning",
              message: "减分成功",
            });
          }
        });
      }
    },

    //领导修改自评分数
    leaderAudit(audit) {
      let auditScope = {
        question: [],
      };
      audit.forEach((item) => {
        let this_self = {
          openid: this.openid,
          fen: item.fen,
          nrid: item.wjid,
        };
        auditScope.question.push(this_self);
      });
      leader_change(auditScope).then((res) => {
        let id = {
          wjid: this.wjid,
        };
        alluserxinxi(id).then((res) => {
          this.userData = res.data;
        });
      });
    },

    //数据分析
    analysis() {
      this.analysis_load = true;
      setTimeout(() => {
        this.$router.push({
          name: "Analysis",
          query: {
            // circleData: this.percentage,
          },
        });
        this.analysis_load = false;
      }, 1000);
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  height: calc(100vh - 84px);
  width: 100%;
  padding: 20px;
  overflow: auto;

  .admin-title {
    width: 100%;
    height: 70px;
    background-color: rgb(255, 255, 255);
    box-sizing: border-box;
    border-bottom: 2px solid;
    border-image: linear-gradient(90deg, rgb(247, 0, 0) 0%, #00afed 100%) 2 2 2
      2;
    //   #8f41e9, #578aef
    padding-left: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    div {
      line-height: 60px;
    }
  }

  .table {
    height: calc(100vh - 194px);
    // background-color: firebrick;
    overflow: auto;
    padding-top: 15px;
    border-right: 2px solid;
    border-image: linear-gradient(
        -180deg,
        #00bbf2 0%,
        rgba(255, 255, 255, 0) 99%
      )
      2 2 2 2;
  }

  // .table {
  //   padding: 10px;
  // }

  .created_form {
    ::v-deep .el-dialog__body {
      height: calc(100vh - 54px);
      overflow: auto;
    }
    // #content {
    //   height: calc(100vh - 114px);
    // }
  }

  .add_dec {
    ::v-deep .el-dialog {
      width: 46% !important;
      .el-divider--horizontal {
        background-color: rgb(183, 33, 33);
        margin-top: 35px;
        .el-divider__text {
          color: rgb(183, 33, 33);
        }
      }
    }
    ::v-deep .el-checkbox__label {
      display: inline-grid;
      white-space: pre-line;
      word-wrap: break-word;
      overflow: hidden;
      line-height: 20px;
    }
  }

  .audit {
    ::v-deep .el-dialog {
      width: 70vw !important;
      margin-top: 10vh !important;
      max-height: 80vh;
      height: 80vh;

      .audit_table {
        height: calc(80vh - 184px);

        .el-input-number {
          width: 100px;

          .el-input__inner {
            padding-left: 0px;
            padding-right: 0px;
          }

          .el-input-number__increase {
            width: 30px;
          }

          .el-input-number__decrease {
            width: 30px;
          }
        }
      }
    }
  }
}
</style>